import datetime, requests, json, pymssql
from bs4 import BeautifulSoup as bs5


def get_kldata():  # 获取当天实时进出客流、进出车辆
    todays = datetime.date.today()
    # 客流数据
    post_url = 'http://10.105.143.10/ReportsAnalysis/RealTimeStay.aspx?MenuId=57'
    # 请求头
    klheader = {
        "Connection": "keep-alive",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
        "Referer": "http://10.105.143.10/ReportsAnalysis/RealTimeStay.aspx?MenuId=57",
        "Cookie": "ASP.NET_SessionId=5tsjj1hw3x5neitzuzovcy0f; UICulture=ZH-CN; UserID=c46fa2f7-863d-4f13-9319-f5360343bfc8; UserName=wangrui; AspNetCookie=CD11907ED9637A0502770061006E006700720075006900000030D920ECF6F2D5010030116E1110F3D50100002F000000A55057357E6A2B8BB00C523DF4F8D9B83626C7E84EE5C7ED54B30C9965450B4E",
    }
    session = requests.session()
    req = session.post(post_url, headers=klheader).text
    soup = bs5(req, 'html.parser')
    keliushuju = soup.find_all('table')
    insum = keliushuju[1].find_all('td')[2].get_text()
    outsum = keliushuju[1].find_all('td')[3].get_text()
    downsum = keliushuju[1].find_all('td')[4].get_text()
    # 车流数据
    carurl = 'http://10.105.146.46/park/home/stat/flow.htm?1593958532428&startTime=' + str(
        todays) + '%2000%3A00%3A00&endTime=' + str(
        todays) + '%2023%3A59%3A59&averageFlag=0&statType=1&loginOperatorName=wangrui&pwd=df6ebbd720969fa1bd08a86b220ae989'
    carheader = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
    }
    carreq = session.get(carurl, headers=carheader)
    carreq.content.decode()
    carlist = json.loads(carreq.text)
    carin = carlist['data']['list'][0]['totalFlowIn']
    carout = carlist['data']['list'][0]['totalFlowOut']
    kl_data = [insum, outsum, downsum, carin, carout]
    return kl_data


def get_park():
    pkheader = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'Connection': 'keep-alive',
        'Cookie': 'JSESSIONID=05E5783B8BB21AAD4E528978B0F61AEB',
        'Host': '10.105.146.46',
        'Referer': 'http://10.105.146.46/park/home/guidance/config/monitor.htm',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
    }
    parkurl = 'http://10.105.146.46/park/home/guidance/getparkingspacecodecount.htm?1594789999675&loginOperatorName=wangrui&pwd=df6ebbd720969fa1bd08a86b220ae989'
    session = requests.session()
    preq = session.get(parkurl, headers=pkheader)
    preq.content.decode()
    pkjson = json.loads(preq.text)
    pkall = pkjson['data']['data']['B1']['floorcount']
    pknull = pkjson['data']['data']['B1']['spacecount']
    return pkall, pknull


def get_timekl():  # 获取当日全天时段广场客流数据
    today = datetime.date.today().strftime('%Y%m%d')
    serverName = '10.105.143.10:1433'
    # 登陆用户名和密码
    userName = 'sa'
    passWord = 'ipva@07'
    # 建立连接并获取cursor
    conn = pymssql.connect(serverName, userName, passWord, "IPVA_S0400_B1")
    cursor = conn.cursor()
    sql = "SELECT InSum FROM Summary_Sixty WHERE SiteKey='P00021' AND DateKey='" + today + "'"
    cursor.execute(sql)
    # 创建测试表 persons，包含字段：ID、name、salesrep
    row = cursor.fetchall()
    return row
    cursor.close()
    conn.close()
